package com.wondertek.poms.dao.po;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.wondertek.oes.sso.common.util.StringUtil;
import com.wondertek.poms.common.constant.CurrentUser;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Proxy;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @author chenjijiang
 * @description
 */
@Data
@Entity
@Table(name = "prd_cont")
@DynamicUpdate
@DynamicInsert
@Proxy(lazy = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PrdCont implements Serializable {
	private static final long serialVersionUID = -3862672993321800078L;

	public static final int BC_STATUS_NOT = 0;// 未播控
	public static final int BC_STATUS_YES = 1;// 播控通过
	public static final int BC_STATUS_NO = 2;// 播控拒绝
	public static final int DISTON_STATUS_NOT = 10; // 待分发
	public static final int DISTON_STATUS_IN = 11; // 分发中
	public static final int DISTON_STATUS_SUCCESS = 12; // 分发成功
	public static final int DISTON_STATUS_FAILURE = 13; // 分发失败
	public static final int DISTOFF_STATUS_NOT = 20; // 待下线
	public static final int DISTOFF_STATUS_IN = 21; // 下线中
	public static final int DISTOFF_STATUS_SUCCESS = 22; // 下线成功
	public static final int DISTOFF_STATUS_FAILURE = 23; // 下线失败

	public static final int PUBON_STATUS_NOT = 10; // 待发布
	public static final int PUBON_STATUS_IN = 11; // 发布中
	public static final int PUBON_STATUS_SUCCESS = 12; // 发布成功
	public static final int PUBON_STATUS_FAILURE = 13; // 发布失败
	public static final int PUBON_STATUS_NORULE = 30; // 发布无分发规则

	public static final int PUBOFF_STATUS_NOT = 20; // 待撤回
	public static final int PUBOFF_STATUS_IN = 21; // 撤回中
	public static final int PUBOFF_STATUS_SUCCESS = 22; // 撤回成功
	public static final int PUBOFF_STATUS_FAILURE = 23; // 撤回失败

	public static final int RECALL_STATUS_NO = 0;// 未发生撤回动作
	public static final int RECALL_STATUS_YES = 1;// 已发生撤回动作

	public static final int FILEACTION_STATUS_ADD = 0; // 新增
	public static final int FILEACTION_STATUS_UPDATE_NOT_MEDIA = 1; // 修改（不修改媒体文件）
	public static final int FILEACTION_STATUS_DELETE = 2; // 删除
	public static final int FILEACTION_STATUS_UPDATE_MEDIA = 3;// 修改（修改媒体文件）

	public static final String EMERGENCY_NO = "0";//非紧急
	public static final String EMERGENCY_YES = "1";//紧急

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "PRD_CONT_SEQ")
	@SequenceGenerator(name = "PRD_CONT_SEQ", allocationSize = 1, sequenceName = "PRD_CONT_SEQ")
	@Column(name = "prd_cont_id")
	private Long prdContId; // 产品主键ID
	@Column(name = "MMS_ID")
	private Long mmsId; // 媒资ID
	@Column(name = "CONTENT_ID")
	private String contentId; // 内容Id
	@Column(name = "PRD_INFO_ID")
	private Long prdInfoId; // 产品包Id
	@Column(name = "BC_STATUS")
	private int bcStatus; // 播控状态
	@Column(name = "BC_PERSON")
	private String bcPerson; // 播控人
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "BC_TIME")
	private Date bcTime; // 播控时间
	
	@Column(name = "PUB_STATUS")
	private int pubStatus; // 发布状态
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "PUB_TIME")
	private Date pubTime; // 发布时间
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "DIST_TIME")
	private Date distTime; // 分发时间
	
	@Column(name = "DIST_STATUS")
	private int distStatus; // 分发状态
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "CREATE_TIME")
	private Date createTime; // 产品内容创建时间
	
	@Column(name = "BC_ID")
	private Long bcId; // 播控方ID
	@Column(name = "FILE_ACTION")
	private int fileAction; // 新增或变更（和content表中的status保持一致）
	@Column(name = "RECALL")
	private int recall; // 0：表示未发生撤回动作 1：表示撤回动作
	@Column(name = "IS_DELETE")
	private int isDelete; // 0:表示未删除，1表示已删除（播控拒绝或不符合时下线删除），2表示手动删除（区分下线删除）， 4被相同媒资的内容替换后删除 5，变更后符合限制规则删除
	@Column(name = "FORM_TYPE")
	private String formType; // 1-视频;2-音频;3-图文;4-复合;5-内容集；6-剧集;7-单集;
	@Column(name = "BC_REFUSE_REASON")
	private String bcRefuseReason; // 播控拒绝原因
	@Column(name = "PUB_WEB_OPER_ID")
	private Long pubWebOperId; // ftp发布操作人
	@Column(name = "PUB_WEB_STATUS")
	private Long pubWebStatus; // ftp发布状态
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "PUB_WEB_TIME")
	private Date pubWebTime;    //ftp发布时间
	
	@Column(name = "PRODUCT_ID")
	private String productId; //计费产品ID   123,456,789
	@Column(name = "IS_EMERGENCY")
	private String isEmergency; //紧急标志
	@Column(name = "PUBLISH_NO_VOMS_STATUS")
	private Integer publishNoVOMSStatus; //不通过VOMS的发布状态
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "PUBLISH_NO_VOMS_TIME")
	private Date publishNoVOMSTime; //不通过VOMS的发布时间
	
	@Column(name = "LEVELVAL")
	private String levelval;
	@Column(name = "RANKVAL")
	private String rankval;
	@Column(name = "LONGTITLE")
	private String longtitle;
	@Column(name = "SHORTTITLE")
	private String shorttitle;
	@Column(name = "SAL_PRODUCT_ID")
	private Long salProductId; // 促销产品Id
	@Column(name = "SAL_CATEGORY")
	private Integer salCategory; //促销类型    1.预售  2.预订   3.限免
	@Column(name = "START_TIME")
	private String startTime; // 开始时间
	@Column(name = "END_TIME")
	private String endTime; //结束时间
	@Column(name = "PRICE")
	private String price;  // 基本资费的产品价格
	@Column(name = "SAL_PRICE")
	private String SalPrice;  //促销的产品价格
	@Column(name = "ORDER_FLAG")
	private String orderFlag;// 0：包月  1：按次    7：免费
	@Column(name = "HITS")
	private Long hits; //点击量
	
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Column(name = "UPDATE_TIME")
	private Date updateTime; //更新时间，用于搜索引擎增量更新
	@Column(name = "IS_UPDATING")
	private Integer isUpdating;//针对剧集壳，0：更新完成，1：更新中
	@Column(name = "PUSH_STATUS")
	private String pushStatus;//推送状态：0-未推送，1-已推送
	@Column(name = "PUSH_TIME")
	private String pushTime;//推送时间
	@Column(name = "OPERATOR")
	private String operator; //节目上线下操作人(发布新操作人)
	@Column(name = "HAS_PUB")
	private Integer hasPub;    //是否曾经发布过(0-未发布过， 1-发布过)，该字段用于剧集单集变更待发布后的缺集情况

	//发布的xml中是否显示productId 1：是 ，0 否
	@Column(name = "IS_SHOW_PRDID")
	private String isShowPrdId;

	//cisStatus必须配合 bcStatus=0 时使用
	/* 当(cisStatus=NULL || cisStatus='2') && bcStatus=0 时调用CIS新增节目接口
	 * 当 (cisStatus=‘1’ || cisStatus='3') && bcStatus=0 && (fileAction=1||fileAction=3)时调用CIS变更节目接口
	 */
	// NULL-新增未同步到CIS， 1-变更, 2-变更未同步到CIS， 3-变更或新增已同步到CIS或没有符合未来电视的媒体文件 4.没有cis所需媒体文件或媒体文件中无播放地址 5.有媒体文件节目更新
	@Column(name = "CIS_STATUS")
	private String cisStatus; //未来电视播控时使用, NULL-新增未同步到CIS， 1-新增已同步到CIS, 2-变更未同步到CIS， 3-变更已同步到CIS 4-节目集且无媒体内容或媒体内容无地址 7-已经删除的
	@Column(name = "EQUITY_ID")
	private String equityId;//权益
	@Column(name = "SELLING_PRICE")
	private String sellingPrice;//售价名称计费
	@Column(name = "IS_PERSON_OPERATION")
	private String isPersonOperation;//售价名称计费是否人工添加  1.是
	@Column(name = "ONTRIAL")
	private Integer onTrial;//是否支持试看,0 不支持 1 支持,节目中该字段值为空时默认为支持；
	/**
     * <b>Fields</b> copyRightObjectID : 版权ID
     */
    @Column(name = "COPYRIGHT_OBJECT_ID")
    private Long copyRightObjectID;


//    /**
//     * <b>Fields</b> longTitle : 长标题
//     */
//    @Column(name = "longtitle")
//    private String longTitle;
//
//    /**
//     * <b>Fields</b> shortTitle : 短标题
//     */
//    @Column(name = "shorttitle")
//    private String shortTitle;
//    
//    @Column(name = "remarks")
//    private String remarks;

	public PrdCont(Content content, PrdInfo prdInfo) {
		this.contentId = content.getContentId();
		this.mmsId = Long.valueOf(content.getAssetId());
		this.bcStatus = PrdCont.BC_STATUS_NOT;
		this.distStatus = PrdCont.DISTON_STATUS_NOT;
		this.pubStatus = PrdCont.PUBON_STATUS_NOT;
		this.recall = PrdCont.RECALL_STATUS_NO;
		this.createTime = new Date();
		this.isEmergency = PrdCont.EMERGENCY_NO;
		this.prdInfoId=prdInfo.getPrdInfoId();
		if (!StringUtil.isNullStr(content.getBcId())) {
			this.bcId = Long.valueOf(content.getBcId().trim());
		}

		this.fileAction = StringUtil.nullToInteger(content.getStatus());
		this.formType = content.getFormType();
		if ((content.getFormType() != null && content.getFormType().trim().equals("6")) ||
				(content.getCategory() != null && content.getCategory().trim().equals("3"))) {
			this.distStatus = PrdCont.DISTON_STATUS_SUCCESS;
		}
		if(content.getFormType() != null && content.getFormType().trim().equals("9")){//专辑
			this.bcStatus = PrdCont.BC_STATUS_YES;
			this.distStatus = PrdCont.DISTON_STATUS_SUCCESS;
			this.pubStatus = PrdCont.PUBON_STATUS_NORULE;
		}

		if(content.getFormType() != null && content.getFormType().trim().equals("14")){//播单
			this.bcStatus = PrdCont.BC_STATUS_YES;
			this.distStatus = PrdCont.DISTON_STATUS_SUCCESS;
			this.pubStatus = PrdCont.PUBON_STATUS_NORULE;
		}
		if(prdInfo.getAutoVomsPublish() != null && prdInfo.getAutoVomsPublish()==1) {
			this.publishNoVOMSStatus = 10;
		}
		if(content.getHasVideo()!=null && !content.getHasVideo()) {//兼容媒资
			this.distStatus = PrdCont.DISTON_STATUS_SUCCESS;
		}
	}

    public PrdCont() {

    }

    public static void putTimeAndUser(PrdCont prdCont) {
        if (prdCont.prdContId == null) {
            if (StringUtils.isBlank(prdCont.getOperator())) {
                prdCont.setOperator(CurrentUser.currUserName());
            }
            prdCont.setCreateTime(new Date());
        }
        if (StringUtils.isBlank(prdCont.getOperator())) {
            prdCont.setOperator(CurrentUser.currUserName());
        }
        prdCont.setUpdateTime(new Date());
    }
}
